﻿using SLAssetSystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

namespace SLUISystem
{
    public class UISystem : Singleton<UISystem>, IInitable
    {
        private GameObject Root;
        private GameObject SimpleCanvas;


        private WindowInfos windowInfos;

        private Dictionary<WindowId, WindowBase> Windows = new Dictionary<WindowId, WindowBase>();
        //private Stack<WindowId, WindowBase> Windows = new Dictionary<WindowId, WindowBase>();


        public void Init()
        {
            Root = new GameObject("UIRoot");
            SimpleCanvas = new GameObject("SimpleCanvas");
            SimpleCanvas.transform.parent = Root.transform;
            Canvas canvas= SimpleCanvas.AddComponent<Canvas>();
            canvas.renderMode = RenderMode.ScreenSpaceOverlay;
            SimpleCanvas.AddComponent<CanvasScaler>();
            SimpleCanvas.AddComponent<GraphicRaycaster>();

            MonoBehaviour.DontDestroyOnLoad(Root);


            CsvData data = AssetSystem.Inst.LoadResAsset<CsvData>(AssetPaths.ConfigsPath, "WindowsInfo",AssetType.CSV);
            windowInfos = new WindowInfos();
            windowInfos.InitData(data);
        }

        private GameObject LoadWindow(WindowId id)
        {
            WindowInfo info = windowInfos.GetWindowInfo((int)id);
            GameObject go= AssetSystem.Inst.LoadResAsset<GameObject>(AssetPaths.WindowPrefabPath, info.Prefab);
            return MonoBehaviour.Instantiate(go);
        }

        public void OpenWindow(WindowId id)
        {
            if (!Windows.ContainsKey(id))
            {
                GameObject go = LoadWindow(id);
                go.transform.parent = SimpleCanvas.transform;
                go.transform.localPosition = Vector3.zero;
                Windows.Add(id, go.GetComponent<WindowBase>()); 
            }
            else
            {
                Windows[id].gameObject.SetActive(true);
            }

            //加入弹窗序列
        }

        public void CloseWindow(WindowId id)
        {

        }

        
    }
}
